import unittest

from buondua.scrappers.picture import PictureParser
from buondua.scrappers.album import AlbumParser
from buondua.scripts.telegram_bot import TelegramBot
from buondua.scripts.telegraph import Telegraph


class TestFeatures(unittest.TestCase):
    '''Функциональные тесты'''

    def test_run_picture_parsing(self):
        from buondua.models import db_proxy, MODELS, orm
        url = 'https://buondua.com/pure-media-vol-270-hina-%ED%9E%88%EB%82%98-sira-%EC%8B%9C%EB%9D%BC-%E2%80%93-x-rated-contract-248-photos-35178'

        db = orm.SqliteDatabase(':memory:')
        db_proxy.initialize(db)
        db.create_tables(MODELS)
        with db:
            picture_parser = PictureParser()
            data = picture_parser.parse(url, do_save=True)

        self.assertEqual(
            data['album']['title'].decode('utf-8'),
            'Pure Media Vol.270: Hina (히나) & Sira (시라) – X-rated Contract (248 photos) '
        )

        self.assertEqual(
            len(data['album']['tags']),
            3
        )

        self.assertEqual(
            data['pictures'][0]['url'].decode('utf-8'),
            'https://i1.wp.com/i0.buondua.com/cdn/35178/Pure-Media-Vol.270-Hina-and-Sira-X-rated-Contract-MissKON.com-001.jpeg'
        )

        self.assertEqual(
            len(data['pictures']),
            248
        )

    def test_run_album_parsing(self):
        url = 'https://buondua.com/hot'

        album_parser = AlbumParser()
        data = album_parser.parse(url)
        data = data.pop('albums')

        self.assertEqual(
            len(data),
            200
        )

        self.assertTrue(
            b'https://buondua.com/' in data[0]
        )

    def test_parsing_pictures_from_albums(self):
        collection_url = 'https://buondua.com/hot'

        album_parser = AlbumParser()
        data = album_parser.parse(collection_url)
        albums = data.pop('albums')

        self.assertEqual(
            len(albums),
            200
        )

        self.assertTrue(
            b'https://buondua.com/' in albums[0]
        )

        for url in albums[:3]:
            picture_parser = PictureParser()
            data = picture_parser.parse(url)
            album = data.pop('album')
            pictures = data.pop('pictures')

            self.assertEqual(
                url,
                album['album_url']
            )

            self.assertTrue(len(pictures) > 0)

    @unittest.skip
    def test_telegram_bot(self):
        telegram_bot = TelegramBot()
        telegram_bot.send_message('hi')

    @unittest.skip
    def test_telegraph(self):
        telegraph = Telegraph()
        d = telegraph.push_album(['https://i1.wp.com/i0.buondua.com/cdn/35178/Pure-Media-Vol.270-Hina-and-Sira-X-rated-Contract-MissKON.com-005.jpeg'], 'test', 'http://example.com/')
        print(d)
